﻿(function() {
  var $, SigninApp, nullGuid, utils, _ref,
    __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  if ((_ref = this.COFE) == null) {
    this.COFE = {};
  }

  utils = COFE.utils;

  $ = jQuery;

  nullGuid = "00000000-0000-0000-0000-000000000000";

  SigninApp = (function(_super) {

    __extends(SigninApp, _super);

    function SigninApp() {
      this.refresh = __bind(this.refresh, this);

      this.signin = __bind(this.signin, this);

      this.signout = __bind(this.signout, this);

      this.OnSignOut = __bind(this.OnSignOut, this);

      this.OnSignIn = __bind(this.OnSignIn, this);
      SigninApp.__super__.constructor.apply(this, arguments);
    }

    SigninApp.prototype.elements = {
      ".signin": "btnSignin",
      ".refresh": "btnRefresh",
      "#signinform": "frmSignin"
    };

    SigninApp.prototype.events = {
      "click .signin": "OnSignIn",
      "click .signout": "OnSignOut"
    };

    SigninApp.prototype.init = function() {
      if (utils.getCookie("cofews_signature") !== "") {
        return this.refresh();
      }
    };

    SigninApp.prototype.callUrl = function(url, crediential, successFunc, errorFunc) {
      var params;
      params = {
        type: "POST",
        url: url,
        success: successFunc,
        error: errorFunc
      };
      if (crediential !== void 0) {
        params.data = crediential;
      }
      return $.ajax(params);
    };

    SigninApp.prototype.OnSignIn = function() {
      var crediential;
      crediential = $("#signin_form").serialize();
      return this.signin(crediential);
    };

    SigninApp.prototype.OnSignOut = function() {
      return this.signout;
    };

    SigninApp.prototype.signout = function() {
      var _this = this;
      return this.callUrl(COFE.signoutUrl, "", function(data, textStatus, jqXHR) {
        return _this.App.trigger("signedOut", "Signout");
      }, function(data, textStatus, jqXHR) {
        return utils.showMessage(jqXHR, "Logout failed");
      });
    };

    SigninApp.prototype.signin = function(crediential) {
      var _this = this;
      return this.callUrl(COFE.signinUrl, crediential, function(data, textStatus, jqXHR) {
        return _this.App.trigger("signedIn", "Signin");
      }, function(data, textStatus, jqXHR) {
        var errorStr;
        errorStr = (jqXHR === "Not Found" ? "Wrong login or password." : "Cannot connect to the server.");
        return utils.showMessage(errorStr, "Login failed");
      });
    };

    SigninApp.prototype.refresh = function() {
      var _this = this;
      return this.callUrl(COFE.signinUrl, "", function(data, textStatus, jqXHR) {
        return _this.App.trigger("signedIn", "Signin");
      }, function(data, textStatus, jqXHR) {
        return console.log("Refresh failed, signin required.");
      });
    };

    return SigninApp;

  })(Spine.Controller);

  COFE.Signin = SigninApp;

}).call(this);
